<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Storage;

/**
 * Class Banner
 *
 * @package App
 * @mixin \Eloquent
 */
class Banner2 extends Model
{
    public $timestamps = false;
    protected $table = 'banner2';


    //跳转到
    const TYPE_WEB = 1;
    const TYPE_APP = 2;
    static $TypeDisplay = [
        self::TYPE_WEB => '跳转到web',
        self::TYPE_APP => '内部跳转',
    ];

    //内部跳转位置
    const SUBTYPE_POSTINGS = 1;
    const SUBTYPE_SHOP = 2;
    static $SubtypeDisplay = [
        self::SUBTYPE_POSTINGS => '帖子',
        self::SUBTYPE_SHOP => '商户',
    ];


    /**
     * 添加banner
     * @param $request
     * @return array
     */
    static function BannerPublish($request)
    {
        $banner = new Banner2();
        //banner
        if ($request->has('purl')) {
            $path = Storage::url(Postings::OssUpdate($request->purl));
            $banner->purl = $path;
        } else {
            return ['status' => 2, 'message' => '图片呢?'];
        }

        $banner->sort = $request->sort;
        $banner->linkurl = $request->linkurl;

        if (!$banner->save()) {
            return ['status' => 2, 'message' => '上传失败'];
        }

        return ['status' => 1, 'message' => '上传成功'];
    }


    /**
     * 修改banner
     * @param $request
     * @param $banner
     * @return array
     */
    static function ModifyBanner($request, $banner)
    {
        //banner
        if ($request->has('purl')) {
            $path = Storage::url(Postings::OssUpdate($request->purl));
            $banner->purl = $path;
        }

        $banner->sort = $request->sort;
        $banner->linkurl = $request->linkurl;

        if (!$banner->save()) {
            return ['status' => 2, 'message' => '上传失败'];
        }

        return ['status' => 1, 'message' => '上传成功'];
    }

}
